<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle" style="display: flex;">
        <!-- 搜索 -->
        <label class="el-form-item-label">出库单号</label>
        <el-input v-model.trim="query.outboundListOrderNo" clearable placeholder="出库单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <label class="el-form-item-label">单据类型</label>
        <el-select v-model="query.otherOutboundOrderType" style="width: 185px;" class="filter-item" placeholder="请选择" @change="toQuery">
          <el-option
            v-for="item in dict.containerOutboundOrder_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">出库单状态</label>
        <el-select v-model="query.status" style="width: 185px;" class="filter-item" placeholder="请选择" @change="toQuery">
          <el-option
            v-for="item in dict.allOrder_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div style="display: flex;">
          <el-button type="success" size="mini" class="filter-item" icon="el-icon-search" @click="toQuery">搜索</el-button>
          <el-button type="warning" size="mini" class="filter-item" icon="el-icon-refresh-right" @click="reset">重置</el-button>
        </div>
      </div>
      <crudOperation :permission="permission" :list="list" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="loading" :data="list" size="small" style="width: 100%;">
        <el-table-column prop="otherOutboundOrderNo" label="出库单号" />
        <el-table-column prop="otherOutboundOrderType" label="单据类型">
          <template slot-scope="scope">
            {{ dict.label.containerOutboundOrder_type[scope.row.otherOutboundOrderType] }}
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="出库单状态">
          <template slot-scope="scope">
            {{ dict.label.allOrder_status[scope.row.orderStatus] }}
          </template>
        </el-table-column>
        <el-table-column prop="outboundTime" label="出库时间" />
        <el-table-column prop="finishTime" label="完成时间" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="materialCode" label="物料编号" />
        <el-table-column prop="materialDescription" label="物料描述" />
        <el-table-column prop="qty" label="数量" />
        <el-table-column prop="realQty" label="实际出库数量" />
        <el-table-column prop="parentPalletCode" label="母托编码" />
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;align-items: center;gap: 10px;">
              <!-- <el-button type="warning" size="mini" @click="cancelOrder(scope.row)">取消出库单</el-button> -->
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将执行取消该出库单,是否继续"
                @confirm="cancelOrder(scope.row, scope.$index)"
              >
                <el-button slot="reference" type="warning" size="mini" :loading="btn.cancel === scope.$index">取消出库单</el-button>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <!-- <pagination /> -->
      <el-pagination
        :page-size.sync="query.size"
        :total="total"
        :current-page.sync="query.current"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler"
        @current-change="pageChangeHandler"
      />
    </div>
  </div>
</template>

<script>
import crudOtherOutboundOrder from '@/api/work/containerOutbound'
import { cancelOrderAPI, getListAPI } from '@/api/work/containerOutbound'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
// import pagination from '@crud/Pagination'

const defaultForm = { id: null, outboundListOrderNo: null, otherOutboundOrderType: null, status: null, outboundTime: null, finishTime: null, createTime: null, createName: null, updateTime: null, updateName: null }
export default {
  name: 'ContainerOutbound',
  components: { crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'otherOutboundOrder', url: 'api/otherOutboundOrder', idField: 'id', sort: 'id,desc', crudMethod: { ...crudOtherOutboundOrder },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: true
      },
      queryOnPresenterCreated: false
    })
  },
  dicts: ['allOrder_status', 'containerOutboundOrder_type'],
  data() {
    return {
      permission: {
        add: ['admin', 'otherOutboundOrder:add', 'otherUser'],
        edit: ['otherOutboundOrder:edit'],
        del: ['admin', 'otherOutboundOrder:del', 'otherUser']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'outboundListOrderNo', display_name: 'other出库单号' },
        { key: 'otherOutboundOrderType', display_name: '单据类型' },
        { key: 'status', display_name: 'other出库单状态（0-未执行，1-开始中，2-执行中，3-已完成）' }
      ],
      query: {
        // outboundListOrderNo: null,
        // otherOutboundOrderType: null,
        // status: null,
        page: 1,
        size: 10
      },
      list: [],
      total: 0,
      loading: false,
      btn: {
        cancel: ''
      }
    }
  },
  mounted() {
    this.toQuery()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    checkDetail(row) {
      this.$router.push(`/outBoundManagement/containerOutboundDetail?otherOutboundOrderNo=${row.otherOutboundOrderNo}&id=${row.id}`)
    },
    async cancelOrder(row, index) {
      // console.log(row)
      try {
        this.btn.cancel = index
        const res = await cancelOrderAPI(row.otherOutboundOrderNo)
        if (res.result === 'OK') {
          this.crud.toQuery()
          this.$message.success('取消出库单成功')
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.cancel = ''
      }
    },
    async toQuery() {
      this.query.page = 1
      this.getTable()
    },
    async getTable() {
      try {
        this.loading = true
        const res = await getListAPI(this.query)
        if (res.result === 'OK') {
          this.list = res.data.content
          this.total = res.data.totalElements
        }
      } finally {
        this.loading = false
      }
    },
    pageChangeHandler(e) {
      this.query.current = e
      this.getTable()
    },
    // 每页条数改变
    sizeChangeHandler(e) {
      this.query.size = e
      this.query.current = 1
      this.getTable()
    },
    reset() {
      this.query = {
        page: 1,
        size: 10
      }
      this.toQuery()
    }
  }
}
</script>

<style scoped>

</style>
